//index.js
//获取应用实例
var api = require("../../api.js"),
    r = "",
    app = getApp();

Page({
    data: {
        tabbar: {},
        pageIndex: 1,
        currentTab: 0, //预设当前项的值
        dataOne: [],
        dataTwo: [],
        dataThree: [],
        dataFour: [],
        loading: false, //第一次加载，设置true  
        loadingComplete: false, //把“没有数据”设为false，隐藏  
        isFromSearch: true,
    },

    onLoad: function(options) {
        r = this;
        var user = getApp().globalData.wxUserInfo;
        // console.log(user)
        app.editTabbar();
        if (options.type) {
            r.setData({
                currentTab: options.type,
                openId: options.openid
            })
        } else {
            r.setData({
                currentTab: 0,
                openId: user.openId
            })
        }
        r.initData(); //初始化数据

    },

    //点击切换 
    clickTab: function(e) {
        var that = this;
        if (this.data.currentTab === e.target.dataset.current) {
            return false;
        } else {
            that.setData({
                currentTab: e.target.dataset.current,
                pageIndex: 1,
                loading: false,
                loadingComplete: false,
                isFromSearch: true,
                hidden: false
            })
            r.initData();
        }
    },
    initData: function() {
        wx.showLoading({
            title: "数据加载中"
        });
        var t = new Object();
        t.pageIndex = 1, t.pageSize = 10;
        t.openId = r.data.openId;
        if (r.data.currentTab == 0) {
            wx.request({
                url: api.class.commentsOfMine,
                data: t,
                success: function(res) {
                    if (res.statusCode == 200) {
                        if (res.data.code == 200) {
                            const n = res.data.data;
                            if (n.length != 0) {
                                var checkStatus = '';
                                n.length < 10 ? checkStatus = false : true;
                                setTimeout(function() {
                                    wx.hideLoading();
                                    r.setData({
                                        dataOne: res.data.data,
                                        loading: checkStatus,
                                        loadingComplete: !checkStatus,
                                        hidden: false
                                    });
                                }, 1000);
                            } else {
                                setTimeout(function() {
                                    r.setData({
                                        dataOne: [],
                                        loadingComplete: false,
                                        hidden: true
                                    });
                                    wx.hideLoading();
                                }, 1000);
                            }


                        }
                    }
                }
            });
        } else if (r.data.currentTab == 1) {
            wx.request({
                url: api.class.historyRecord,
                data: t,
                success: function(res) {
                    if (res.statusCode == 200) {
                        if (res.data.code == 200) {
                            const n = res.data.data;
                            if (n.length != 0) {
                                console.log(n.length)
                                var checkStatus = '';
                                checkStatus = n.length < 6 ? false : true;
                                console.log(checkStatus)
                                setTimeout(function() {
                                    wx.hideLoading();
                                    r.setData({
                                        dataTwo: res.data.data,
                                        loading: checkStatus,
                                        loadingComplete: !checkStatus,
                                        hidden: false
                                    });
                                }, 1000);
                            } else {
                                setTimeout(function() {
                                    r.setData({
                                        dataTwo: [],
                                        loadingComplete: false,
                                        hidden: true
                                    });
                                    wx.hideLoading();
                                }, 1000);
                            }
                        }
                    }

                }
            });
        } else if (r.data.currentTab == 2) {
            wx.request({
                url: api.class.actOfMineParticipateIn,
                data: t,
                success: function(res) {
                    if (res.statusCode == 200) {
                        if (res.data.code == 200) {
                            const n = res.data.data;
                            if (n.length != 0) {
                                var checkStatus = '';
                                checkStatus = n.length < 10 ? false : true;
                                setTimeout(function() {
                                    wx.hideLoading();
                                    r.setData({
                                        dataThree: res.data.data,
                                        // loading:true,
                                        loading: checkStatus,
                                        loadingComplete: !checkStatus,
                                        hidden: false
                                    });
                                }, 1000);
                            } else {
                                setTimeout(function() {
                                    r.setData({
                                        dataThree: [],
                                        loadingComplete: false,
                                        hidden: true
                                    });
                                    wx.hideLoading();
                                }, 1000);
                            }
                        }
                    }

                }
            });
        } else {
            wx.request({
                url: api.class.mineCollection,
                data: t,
                success: function(res) {
                    if (res.statusCode == 200) {
                        if (res.data.code == 200) {
                            const n = res.data.data;
                            if (n.length != 0) {

                                var checkStatus = '';
                                checkStatus = n.length < 10 ? false : true;
                                setTimeout(function() {
                                    wx.hideLoading();
                                    r.setData({
                                        dataFour: res.data.data,
                                        loading: checkStatus,
                                        loadingComplete: !checkStatus,
                                        hidden: false
                                    });
                                }, 1000);
                            } else {
                                setTimeout(function() {
                                    r.setData({
                                        dataFour: [],
                                        loadingComplete: false,
                                        hidden: true
                                    });
                                    wx.hideLoading();
                                }, 1000);
                            }

                        }
                    }
                }
            });
        }

    },
    loading: function() {
        var t = new Object();
        var dataList = [];
        t.pageIndex = r.data.pageIndex, t.pageSize = 10, t.openId = r.data.openId;
        if (r.data.currentTab == 0) {
            wx.request({
                url: api.class.commentsOfMine,
                data: t,
                success: function(res) {
                    if (res.statusCode == 200) {
                        if (res.data.code == 200) {
                            var n = res.data.data;
                            if (n.length > 0) {
                                r.data.isFromSearch ? dataList = n : dataList = r.data.dataOne.concat(n)
                                setTimeout(() => {
                                    r.setData({
                                        dataOne: dataList,
                                        loading: true
                                    });
                                }, 500)
                            } else {
                                setTimeout(() => {
                                    r.setData({
                                        loadingComplete: true,
                                        loading: false
                                    })
                                }, 500)

                            }

                        }
                    }

                }
            });
        } else if (r.data.currentTab == 1) {
            wx.request({
                url: api.class.historyRecord,
                data: t,
                success: function(res) {
                    if (res.statusCode == 200) {
                        if (res.data.code == 200) {
                            var n = res.data.data;
                            if (n.length > 0) {
                                r.data.isFromSearch ? dataList = n : dataList = r.data.dataTwo.concat(n)
                                setTimeout(() => {
                                    r.setData({
                                        dataTwo: dataList,
                                        loading: true
                                    });
                                }, 500)
                            } else {
                                setTimeout(() => {
                                    r.setData({
                                        loadingComplete: true,
                                        loading: false
                                    })
                                }, 500)

                            }

                        }
                    }

                }
            });
        } else if (r.data.currentTab == 2) {
            wx.request({
                url: api.class.actOfMineParticipateIn,
                data: t,
                success: function(res) {
                    if (res.statusCode == 200) {
                        if (res.data.code == 200) {
                            var n = res.data.data;
                            if (n.length > 0) {
                                r.data.isFromSearch ? dataList = n : dataList = r.data.dataThree.concat(n)
                                setTimeout(() => {
                                    r.setData({
                                        dataThree: dataList,
                                        loading: true
                                    });
                                }, 500)
                            } else {
                                setTimeout(() => {
                                    r.setData({
                                        loadingComplete: true,
                                        loading: false
                                    })
                                }, 500)

                            }

                        }
                    }

                }
            });
        } else {
            wx.request({
                url: api.class.mineCollection,
                data: t,
                success: function(res) {
                    if (res.statusCode == 200) {
                        if (res.data.code == 200) {
                            var n = res.data.data;
                            if (n.length > 0) {
                                r.data.isFromSearch ? dataList = n : dataList = r.data.dataFour.concat(n)
                                setTimeout(() => {
                                    r.setData({
                                        dataFour: dataList,
                                        loading: true
                                    });
                                }, 500)
                            } else {
                                setTimeout(() => {
                                    r.setData({
                                        loadingComplete: true,
                                        loading: false
                                    })
                                }, 500)

                            }

                        }
                    }

                }
            });
        }

    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        let _that = this
        console.log(_that.data)
        if (_that.data.loading && !_that.data.loadingComplete) {
            _that.setData({
                pageIndex: _that.data.pageIndex + 1,
                isFromSearch: false
            });
            _that.loading();
        };
    },
    middle: function(e) {
        const data = e.currentTarget.dataset.list;
        console.log(data)
        if (data.type == 0) {
            wx.navigateTo({
                url: "../activedetial/activedetial?id=" + data.otherId

            });
        } else if (data.type == 1) {
            wx.navigateTo({
                url: "../art/art?id=" + data.otherId
            });
        } else if (data.type == 2) {
            wx.navigateTo({
                url: "../classdetail/classdetail?id=" + data.otherId
            });
        } else if (data.type == 3) {
            wx.navigateTo({
                url: "../discoverdetail/discoverdetail?id=" + data.otherId
            });
        } else if (data.type == 4) {
            wx.navigateTo({
                url: "../audiodetail/audiodetail?id=" + data.otherId
            });
        }
    },
    goArt(e) {
        console.log(e)
        const data = e.currentTarget.dataset;

        if (data.type == 0) {
            wx.navigateTo({
                url: "../activedetial/activedetial?id=" + data.id
            });
        } else if (data.type == 1) {
            wx.navigateTo({
                url: "../art/art?id=" + data.id
            });
        } else if (data.type == 2) {
            wx.navigateTo({
                url: "../classdetail/classdetail?id=" + data.id
            });
        }
    },
    goDetails(e) {
        const data = e.currentTarget.dataset;
        wx.navigateTo({
            url: "../activedetial/activedetial?id=" + data.id

        });
    }

});